<?php

namespace app\admin\model\game;

use think\Model;


class Ip extends Model
{

    

    

    // 表名
    protected $name = 'game_ip';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'updateTime_text'
    ];
    

    



    public function getUpdatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['updateTime']) ? $data['updateTime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setUpdatetimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function getInfoByIp($ip)
    {
        $res = $this->where("ip",$ip)->find();
        return $res;
    }
    

     public function addData($ip)
    {
        $time = time();
        $data = [
            "ip" => $ip,
            "num"=> 0,
            "updateTime" => $time
        ];            

        $res = $this->data($data)->save();
        return $res;
    }

    public function updateInfoByIp($ip,$num)
    {

        $time = time();
        $data = [
            "num"=> $num,
            "updateTime" => $time
        ];   
        $where = [
            "ip" => ["=",$ip]
        ];

        $res = $this->where($where)->update($data);
    }

    public function ipSetIn($ip){
        $where = [
            "ip" => ["=",$ip]
        ];

        $res = $this->where($where)->setInc("num");
    }

}
